package com.google.project.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.project.client.Admin.ADMEntrypoint;
import com.google.project.client.Director.DRTEntrypoint;
import com.google.project.client.HumanResources.HMEntrypoint;
import com.google.project.client.Operator.OPTEntrypoint;
import com.google.project.client.ProjectManager.PMEntrypoint;
import com.google.project.client.Service.LoginService;
import com.google.project.client.Service.LoginServiceAsync;
import com.google.project.shared.UsersDTO;

public class Login implements EntryPoint {
	private final static LoginServiceAsync loginService = GWT
			.create(LoginService.class);

	public static RootPanel rootPanel;

	public void onModuleLoad() {
		exportStaticMethod();
		rootPanel = RootPanel.get();
		rootPanel.clear();
		Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
			public void execute() {
			
			}
		});

		HTMLPanel panel = new HTMLPanel("");
		panel.setStyleName("htmlpanel");
		rootPanel.add(panel, 0, 0);
		panel.setSize("1389px", "802px");
		
				HTMLPanel panel_1 = new HTMLPanel(
						" <body><div id=\"headLogin\"></div><div id=\"wrapper\">\r\n        <div id=\"header\">\r\n        <h1 id=\"logo\"><a href=\"#\">Architect</a></h1>\r\n        \r\n        <ul id=\"mainNav\">\r\n        </ul>\r\n        </div><!--End #header-->\r\n        \r\n        <div id=\"featured\">\r\n\r\n\r\n<div id=\"signin-box\">\r\n  <h2>Đăng nhập <strong></strong></h2>\r\n <br>\r\n<div id=\"email-div\">\r\n  <label for=\"Email\"><strong class=\"email-label\">Tài khoản</strong></label>\r\n  <input  type=\"email\" spellcheck=\"false\"  \r\n  name=\"Email\" id=\"userName\" value=\"\"\r\n\r\n  \r\n\r\n      \r\n    >\r\n</div>\r\n<div id=\"passwd-div\">\r\n  <label for=\"Passwd\"><strong id=\"passwd-label\">Mật khẩu</strong></label>\r\n  <input  type=\"password\" name=\"Passwd\" id=\"Passed\"\r\n  >\r\n</div>\r\n  <input type=\"button\" id=\"g-button g-button-submit\" name=\"signIn\" id=\"signIn\"\r\n      value=\"Đăng nhập\" onclick=\"window.Callee();\">\r\n  <input type=\"hidden\"  name=\"rmShown\" value=\"1\">\r\n  </form>\r\n</div>\r\n\r\n    <div id=\"anh\">    \r\n        <img class=\"featuredImg\" src=\"images/featured.png\" alt=\"Featured Image\" />\r\n        </div>\r\n        </div><!--End #featured-->\r\n\r\n    </div><!--End #wrapper--></body>");
				rootPanel.add(panel_1, -1, 26);
				panel_1.setSize("1376px", "785px");
		
	}
	public static native  void clearText ()
	/*-{
	 $doc.getElementById("userName").innerHTML=""; 
	 $doc.getElementById("Passed").innerHTML=""; 
	  }-*/;
	public static native  String getUserName ()
	/*-{
	 return $doc.getElementById("userName").value; 
	  }-*/;
	public static native  String getPassword ()
	/*-{
	 return $doc.getElementById("Passed").value; 
	  }-*/;
	public static void test()
	{
		String userName=getUserName() ;
		final String password=getPassword();
		loginService.login(userName,
				password, new AsyncCallback<String>() {

					@Override
					public void onSuccess(String result) {
						loginService
								.getCurrentUserDTO(new AsyncCallback<UsersDTO>() {
									@Override
									public void onFailure(
											Throwable caught) {

									}

									@Override
									public void onSuccess(
											UsersDTO result) {
										if (result.getUserGroup() == 1) {
											rootPanel.clear();
											DRTEntrypoint drt = new DRTEntrypoint();
											drt.onModuleLoad();
										} else if (result
												.getUserGroup() == 2) {
											rootPanel.clear();
											PMEntrypoint pm = new PMEntrypoint();
											pm.onModuleLoad();
										} else if (result
												.getUserGroup() == 3) {
											rootPanel.clear();
											OPTEntrypoint opt = new OPTEntrypoint();
											opt.onModuleLoad();
										} else if (result
												.getUserGroup() == 4) {
											rootPanel.clear();
											HMEntrypoint hm = new HMEntrypoint();
											hm.onModuleLoad();
										} else if (result
												.getUserGroup() == 6) {
											rootPanel.clear();
											ADMEntrypoint adm = new ADMEntrypoint();
											adm.onModuleLoad();
										} else if (result
												.getUserGroup() == -1) {
											Window
													.alert("Bạn đã nhập sai Tài khoản hoặc Mật khẩu! Hãy nhập lại!");
											clearText();
											//password.setText("");
											//txtUserName.setText("");
										}
									}
								});

					}

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("có lỗi xảy ra");

					}
				});
	}
	 

	public static native void exportStaticMethod() /*-{
	
	   $wnd.Callee =
	      $entry(@com.google.project.client.Login::test());
	}-*/;
}
